
package xib.parserTools.data;

import xib.parserTools.interfaces.ISpeechBubbleData;
import xib.utils.XibDataException;

/**
 * Implementation from the interface ISpeechBubbleData, this implementation is
 * used to save the data from the source before to be processed by the core.
 * @author xavierds
 */
public final class SpeechBubbleData implements ISpeechBubbleData{

    /**
     * Private field with the value of the imageIdentifier.
     */
    private String imageIdentifier;

    /**
     * Private field with the text of the imageIdentifier.
     */
    private String text;

    /**
     * Private fields with the values of the location from the X and Y axis.
     */
    private int locationX, locationY;

    /**
     * Private fields with the values of the width and height.
     */
    private int width, height;

    /**
     * Private field with the value of the type of SpeechBubble.
     */
    private SpeechBubbleType speechBubbleType;

    /**
     * Field who indicates if the panel was validate.
     */
    private boolean validate;

    /**
     * Constructs a new SpeechBubbleData, the SpeechBubbleType is initialized
     * like a SpeechBubbleType.DefaultLanguage. All others fields are initialized
     * with null values.
     */
    public SpeechBubbleData(){
        imageIdentifier = "";
        text = "";
        speechBubbleType = SpeechBubbleType.DefaultLanguage;
    }

    public String getImageIdentifier(){
        if(imageIdentifier.equals("") &&
                speechBubbleType.equals(SpeechBubbleType.OtherLanguage))
            throwXibDataException(new IllegalStateException("The  " +
                    "imageIdentifier has not been initialized properly."));
        return imageIdentifier;
    }

    public String getText(){
        return text;
    }

    public SpeechBubbleType getSpeechBubbleType(){
        return speechBubbleType;
    }

    public int getLocationX() {
        return locationX;
    }

    public int getLocationY() {
        return locationY;
    }

    public int getWidth() {
        if(width < 1)
            throwXibDataException(new IllegalStateException("The width " +
                    "has not been initialized properly."));
        return width;
    }

    public int getHeight() {
        if(height < 1)
            throwXibDataException(new IllegalStateException("The height " +
                    "has not been initialized properly."));
        return height;
    }

    public void setImageIdentifier(String imageIdentifier){
        validate = false;
        if(imageIdentifier == null)
            throwXibDataException(new NullPointerException("The " +
                    "imageIdentifier can't be null."));
        if(imageIdentifier.equals(""))
            throwXibDataException(new IllegalArgumentException("The " +
                    "imageIdentifier must be different from the empty string."));
        this.imageIdentifier = imageIdentifier;
    }

    public void setText(String text){
        validate = false;
        if(text == null)
            throwXibDataException(new NullPointerException("The text " +
                    "can't be null."));
        this.text = text;
    }

    public void setSpeechBubbleType(SpeechBubbleType speechBubbleType){
        validate = false;
        if(speechBubbleType == null)
            throwXibDataException(new NullPointerException("The " +
                    "speechBubbleType can't be null."));
        this.speechBubbleType = speechBubbleType;
    }

    public void setLocationXY(int x, int y){
        validate = false;
        if(x < 0 || y < 0)
            throwXibDataException(new IllegalArgumentException("The x and the " +
                    "y can't be less than 0."));
        this.locationX = x;
        this.locationY = y;
    }

    public void setWidthAndHeight(int width, int height){
        validate = false;
        if(width < 1 || height < 1)
            throwXibDataException(new IllegalArgumentException("The width " +
                    "and the height must be greater than 0."));
        this.width = width;
        this.height = height;
    }

    public boolean validate(){
        if(!validate){
            if(height ==  0 || width == 0)
                throwXibDataException(new IllegalStateException("The width or" +
                        " the height have not been initialized properly."));
            if(speechBubbleType.equals(SpeechBubbleType.OtherLanguage) &&
                    imageIdentifier.equals(""))
                throwXibDataException(new IllegalStateException("The " +
                        "imageIdentifier has not been initialized properly."));
            validate = true;
        }
        return true;
    }

    /**
     * Throw a new XibDataException with the cause from the given.
     * @param cause
     */
    private void throwXibDataException(Throwable cause){
        throw new XibDataException("Error in the internal data of the source, " +
                "in the SpeechBubbleData."
                , cause);
    }
}
